package com.suyang.syt.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suyang.syt.dao.interfaces.WeixinGroupDao;
import com.suyang.syt.model.WeixinGroup;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.PagerEasyUI;
import com.suyang.syt.service.interfaces.WeixinGroupService;
import com.suyang.syt.util.EmptyUtil;
import com.suyang.syt.weixin.WeixinKit;

@Service
public class WeixinGroupServiceImpl implements WeixinGroupService {

	@Autowired
	private WeixinGroupDao weixinGroupDao;

	@Override
	public PagerEasyUI<WeixinGroup> listByEasyUI(int page, int rows, String sort, String order) throws Exception {
		StringBuilder hql = new StringBuilder(" from WeixinGroup wg where 1=1 ");
		if (EmptyUtil.isNotEmpty(sort)) {
			hql.append(" order by wg.").append(sort);
		}
		if (EmptyUtil.isNotEmpty(order)) {
			hql.append(" ").append(order);
		}
		PagerEasyUI<WeixinGroup> easyUIPager = new PagerEasyUI<WeixinGroup>();
		List<WeixinGroup> list = weixinGroupDao.findByEasyUI(hql.toString(), page, rows);
		if (EmptyUtil.isNotEmpty(list)) {
			easyUIPager.setRows(list);
			easyUIPager.setTotal((Long) weixinGroupDao.queryByHql(" select count(*) " + hql.substring(0, hql.indexOf("order by"))));
		}
		return easyUIPager;
	}

	@Override
	public AjaxObj add(String name) throws Exception {
		if (EmptyUtil.isNotEmpty(name)) {
			weixinGroupDao.add(WeixinKit.createGroup(name));
			return new AjaxObj(1, "微信组增加成功!");
		}
		return new AjaxObj(0, "微信组增加失败,组名不正确!");
	}

	@Override
	public AjaxObj updateSync() throws Exception {
		List<WeixinGroup> list = WeixinKit.listGroup();
		if (EmptyUtil.isNotEmpty(list)) {
			weixinGroupDao.clear();
			for (WeixinGroup weixinGroup : list) {
				weixinGroupDao.add(weixinGroup);
			}
			return new AjaxObj(1, "微信组同步成功!");
		}
		return new AjaxObj(0, "微信组同步失败,请稍后再试!");
	}

	@Override
	public AjaxObj deletes(String ids) throws Exception {
		if (EmptyUtil.isEmpty(ids)) {
			return new AjaxObj(0, "请选择您要删除的分组!");
		}
		String[] idsArr = ids.split(",");
		if (EmptyUtil.isNotEmpty(idsArr)) {
			boolean flag = true;
			String groupIds = "";
			for (String str : idsArr) {
				int id = Integer.parseInt(str);
				if (id < 100) {
					continue;
				}
				groupIds += str + ",";
				flag = WeixinKit.deleteGroup(id);
				if (!flag) {
					updateSync();
					return new AjaxObj(0, "删除分组失败,系统已经重置分组,请稍后刷新页面再试!");
				}
			}
			weixinGroupDao.deletes(groupIds.substring(0, groupIds.length() - 1));
			return new AjaxObj(1, "删除分组成功!");
		}
		return new AjaxObj(0, "删除分组失败,请稍后再试!");
	}

	@Override
	public AjaxObj update(int id, String name) throws Exception {
		if (id < 100) {
			return new AjaxObj(0, "系统分组不允许修改!");
		}
		if (EmptyUtil.isEmpty(name)) {
			return new AjaxObj(0, "分组名称不能为空!");
		}
		if (WeixinKit.updateGroup(id, name)) {
			weixinGroupDao.updateGroup(id, name);
			return new AjaxObj(1, "组名更新成功!");
		}
		return new AjaxObj(0, "组名更新失败,请稍后再试!");
	}

	@Override
	public List<WeixinGroup> listAll() throws Exception {
		String hql = " from WeixinGroup order by id ";
		return weixinGroupDao.list(hql);
	}

}
